﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VietKinsNutrition.Bussiness;
using VietKinsNutrition.ThuVien;

namespace VietKinsNutrition.UserControl
{
    public partial class Thuvienthucpham : DevExpress.XtraEditors.XtraUserControl
    {
        int idLueNhomTP = -1;
        ClsTblDm_ThanhphandingduongcuathucanBLL ClsTblDm_Thanhphandingduongcuathucanbll = new ClsTblDm_ThanhphandingduongcuathucanBLL();
        
        public Thuvienthucpham()
        {
            InitializeComponent();
        }

        private void Thuvienthucpham_Load(object sender, EventArgs e)
        {
            ClsTblDm_Thanhphandingduongcuathucanbll.FillComboBox(this.lueNhomTP);
            ClsTblDm_Thanhphandingduongcuathucanbll.LoadDataGridView(this.gridControl1);
        }

        private void lueNhomTP_EditValueChanged(object sender, EventArgs e)
        {
            if (this.lueNhomTP.EditValue != null && this.lueNhomTP.EditValue.ToString() != "")
            {
                idLueNhomTP = (int)lueNhomTP.EditValue;
                ClsTblDm_Thanhphandingduongcuathucanbll.LoadDataGridView(this.gridControl1, Convert.ToInt32(this.lueNhomTP.EditValue));
            }
        }

        private void txtTen_TextChanged(object sender, EventArgs e)
        {
            if (this.lueNhomTP.EditValue == null || this.lueNhomTP.EditValue.ToString() == "")
            {
                ClsTblDm_Thanhphandingduongcuathucanbll.Bind_Grid(this.gridControl1, this.txtTen.Text);
            }
            else
            {
                ClsTblDm_Thanhphandingduongcuathucanbll.Bind_Grid(this.gridControl1, this.txtTen.Text, Convert.ToInt32(this.lueNhomTP.EditValue));
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.lueNhomTP.EditValue = null;
            this.txtTen.Text = "";
            ClsTblDm_Thanhphandingduongcuathucanbll.LoadDataGridView(this.gridControl1);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            ClsTblDm_Thanhphandingduongcuathucanbll.Delete();
            btnRefresh_Click(sender, e);
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            ClsTblDm_Thanhphandingduongcuathucanbll.DataGridView_Click(this.advBandedGridView1);
        }

        string sTieude, sTenso, sTentruong, sTenphong; DataRow dr;
        private void btnIn_Click(object sender, EventArgs e)
        {
            if (advBandedGridView1.RowCount <= 0)
                return;
            ClsTblTruonghocBLL ClsTblTruonghocbll = new ClsTblTruonghocBLL();
            DataTable Tbl = ClsTblTruonghocbll.ReturnTblTRuong();
            ClsExport2ExcelBLL ClsExport2Excelbll = new ClsExport2ExcelBLL();

            if (Tbl.Rows.Count > 0)
            {
                dr = Tbl.Rows[0];
                sTenso = dr["TensoGD"].ToString();
                sTentruong = dr["Tentruong"].ToString();
                sTenphong = dr["TenphongGD"].ToString();

                if (this.lueNhomTP.EditValue == null || this.lueNhomTP.EditValue.ToString() == "")
                {
                    sTieude = "DANH SÁCH THỰC PHẨM TRONG THƯ VIỆN";
                }
                else
                {
                    sTieude = "DANH SÁCH THỰC PHẨM NHÓM " + this.lueNhomTP.Text.ToString().ToUpper();
                }
            }
            #region Mẫu Excel
            DataTable tg = (DataTable)(gridControl1.DataSource);
            try
            {
                tg.Columns.Remove("Id");
                tg.Columns.Remove("nhom");
                tg.Columns.Remove("nam");
                tg.Columns.Remove("nguyenlieu");
            }
            catch { }

            System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("VietKinsNutrition.Resources."+"danh sach thu vien thuc pham.xls");
            ClsDanhsachTre.Ds_thucpham(stream, "danh sach thu vien thuc pham.xls", sTenso, sTenphong, sTentruong, sTieude, tg, "");
            #endregion
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            FrmTblDm_Capnhatthucpham frmThuvien = new FrmTblDm_Capnhatthucpham("", "", this.lueNhomTP.Text, idLueNhomTP);
            frmThuvien.CallFunction = new FrmTblDm_Capnhatthucpham.GetFunction(LoadData);
            frmThuvien.ShowDialog();
        }

        public void LoadData()
        {
            if (this.lueNhomTP.EditValue == null || Convert.ToInt32(this.lueNhomTP.EditValue) == 0)
            {
                ClsTblDm_Thanhphandingduongcuathucanbll.LoadDataGridView(this.gridControl1);
            }
            else
                ClsTblDm_Thanhphandingduongcuathucanbll.LoadDataGridView(this.gridControl1, Convert.ToInt32(this.lueNhomTP.EditValue));
        }

        private void advBandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string Temp, Name;
                if (this.advBandedGridView1.GetFocusedRowCellValue("Id").ToString() == null)
                {
                    Temp = "";
                    Name = "";
                }
                else
                {
                    Temp = this.advBandedGridView1.GetFocusedRowCellValue("Id").ToString();
                    Name = this.advBandedGridView1.GetFocusedRowCellValue("Tenthucan").ToString();
                }

                FrmTblDm_Capnhatthucpham Frm = new FrmTblDm_Capnhatthucpham(Name, Temp, this.lueNhomTP.Text, -1);
                Frm.CallFunction = new FrmTblDm_Capnhatthucpham.GetFunction(LoadData);
                Frm.ShowDialog();
            }
            catch { }
        }
    }
}
